package com.rock.code.system.index.controller;

import com.rock.code.base.controller.BaseController;
import com.rock.code.base.domain.AjaxJson;
import com.rock.code.base.domain.Constants;
import com.rock.code.common.exceptions.SystemException;
import com.rock.code.system.index.service.IndexService;
import com.rock.code.system.module.domain.TbSysModule;
import com.rock.code.system.user.domain.TbSysLoginmsg;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/index")
public class IndexController extends BaseController {

    @Resource
    private IndexService indexServiceImpl;

    @RequestMapping("")
    public String dispatch() throws Exception {
        return "system/index/index";
    }

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String home() throws Exception {
        return "system/index/homePage";
    }

    @RequestMapping(value="/jumpPage/{url}")
    public String jumpPage(@PathVariable("url") String url) throws Exception {
        this.request.setAttribute("jumpUrl", URLDecoder.decode(url,"UTF-8"));
        return "system/index/content";
    }

    @ResponseBody
    @RequestMapping(value = "/findSubModule", method = RequestMethod.POST)
    public AjaxJson findSubModule() throws Exception{
        TbSysLoginmsg sessionUser = getSessionUser();
        Map<String, Object> param = this.getRequestMapSingle();
        param.put("isDelete", Constants.DELETE_N);
        param.put("loginCode", sessionUser.getLoginCode());
        try {
            List<TbSysModule> list = indexServiceImpl.queryUserRoleModuleByParaMap(param);
            ajaxJson.setResult(list);
        } catch (Exception e){
            e.printStackTrace();
            throw new SystemException(e);
        }
        return ajaxJson;
    }

}
